<template>
  <div>
    <div class="container">
      <div class="category-page">
        <h1 class="category-title">{{ categoryName }}</h1>
        
        <div class="filter-bar">
          <div class="filter-item">
            <span class="label">排序：</span>
            <el-radio-group v-model="sortType" size="small">
              <el-radio-button label="default">默认</el-radio-button>
              <el-radio-button label="sales">销量</el-radio-button>
              <el-radio-button label="price">价格</el-radio-button>
              <el-radio-button label="comments">评论数</el-radio-button>
            </el-radio-group>
          </div>
          <div class="filter-item">
            <el-checkbox v-model="onlyDiscount" size="small">只看特惠</el-checkbox>
            <el-checkbox v-model="onlyInStock" size="small">只看有货</el-checkbox>
          </div>
        </div>
        
        <div v-if="loading" class="loading-container">
          <el-skeleton :rows="5" animated />
        </div>
        
        <div v-else-if="products.length === 0" class="empty-container">
          <el-empty description="暂无相关商品" />
        </div>
        
        <div v-else class="product-grid">
          <div v-for="product in products" :key="product.id" class="product-card">
            <div class="product-img">
              <img :src="product.imgUrl" :alt="product.title">
            </div>
            <div class="product-info">
              <h3 class="product-title">{{ product.title }}</h3>
              <div class="product-price">¥{{ product.price }}</div>
              <div class="product-actions">
                <el-button type="primary" size="small">查看详情</el-button>
                <el-button type="danger" size="small">加入购物车</el-button>
              </div>
            </div>
          </div>
        </div>
        
        <div class="pagination-container">
          <el-pagination
            v-model:current-page="currentPage"
            :page-size="pageSize"
            :total="totalItems"
            layout="total, prev, pager, next"
            @current-change="handlePageChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute } from 'vue-router'

const route = useRoute()

// 获取路由参数
const categoryId = computed(() => route.query.id || '')
const categoryName = computed(() => route.query.name || '商品分类')

// 状态管理
const loading = ref(true)
const sortType = ref('default')
const onlyDiscount = ref(false)
const onlyInStock = ref(true)
const currentPage = ref(1)
const pageSize = ref(20)
const totalItems = ref(0)

// 模拟商品数据
const productList = ref([])
const products = computed(() => {
  // 这里可以根据筛选条件过滤商品
  return productList.value
})

// 模拟加载数据
const fetchCategoryProducts = async () => {
  loading.value = true
  
  try {
    // 模拟 API 请求延迟
    await new Promise(resolve => setTimeout(resolve, 800))
    
    // 模拟产品数据
    productList.value = Array(12).fill().map((_, index) => ({
      id: `${categoryId.value}-${index}`,
      title: `${categoryName.value}商品${index + 1}`,
      price: Math.floor(Math.random() * 5000) + 100,
      imgUrl: `https://picsum.photos/id/${index + 10}/300/300`,
      discount: Math.random() > 0.7,
      inStock: Math.random() > 0.2,
    }))
    
    totalItems.value = 100 // 模拟总商品数
  } catch (error) {
    console.error('获取分类商品失败', error)
    productList.value = []
  } finally {
    loading.value = false
  }
}

// 处理分页变化
const handlePageChange = (page) => {
  currentPage.value = page
  fetchCategoryProducts()
}

// 页面加载时获取数据
onMounted(() => {
  fetchCategoryProducts()
})
</script>

<style lang="scss" scoped>
.category-page {
  padding: 20px 0;
  
  .category-title {
    font-size: 24px;
    font-weight: 500;
    color: #333;
    margin-bottom: 20px;
  }
  
  .filter-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px;
    background-color: #f5f5f5;
    border-radius: 4px;
    margin-bottom: 20px;
    
    .filter-item {
      display: flex;
      align-items: center;
      
      .label {
        margin-right: 10px;
      }
    }
  }
  
  .loading-container, .empty-container {
    min-height: 400px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .product-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 20px;
    margin-bottom: 30px;
    
    .product-card {
      border: 1px solid #e0e0e0;
      border-radius: 4px;
      overflow: hidden;
      transition: transform 0.3s, box-shadow 0.3s;
      
      &:hover {
        transform: translateY(-5px);
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
      }
      
      .product-img {
        height: 200px;
        overflow: hidden;
        
        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
          transition: transform 0.5s;
          
          &:hover {
            transform: scale(1.1);
          }
        }
      }
      
      .product-info {
        padding: 15px;
        
        .product-title {
          font-size: 16px;
          margin-bottom: 10px;
          height: 44px;
          overflow: hidden;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 2;
        }
        
        .product-price {
          color: var(--primary-color);
          font-size: 18px;
          font-weight: bold;
          margin-bottom: 10px;
        }
        
        .product-actions {
          display: flex;
          justify-content: space-between;
        }
      }
    }
  }
  
  .pagination-container {
    display: flex;
    justify-content: center;
    margin: 30px 0;
  }
}
</style> 